/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.form;

import org.rails.extjs.Option;
import org.rails.extjs.Template;
import org.rails.extjs.data.Store;

public class ComboBox extends TriggerField {
	
	public static final String MODE_REMOTE = "remote";
	public static final String MODE_LOCAL = "local";
	
	public static final String TRIGGER_ACTION_QUERY = "query";
	public static final String TRIGGER_ACTION_ALL = "all";

	public ComboBox() {
		super();
	}

	public ComboBox(Option configOptions) {
		super(configOptions);
	}
	
	public ComboBox(String name) {
		super(name);
	}
	
	public ComboBox(String name, String fieldLabel) {
		super(name, fieldLabel);
	}
	
	public ComboBox(String name, String fieldLabel,Object value) {
		super(name, fieldLabel,value);
	}

	public ComboBox(String name, String fieldLabel,Store store) {
		super(name, fieldLabel);
		setStore(store);
	}

	public ComboBox(String name, String fieldLabel, Object value,Store store) {
		super(name, fieldLabel, value);
		setStore(store);
	}
	
	public void setAllQuery(String allQuery){
		addOption("allQuery", allQuery);
	}
	
	public String getAllQuery(){
		return getString("allQuery","");
	}
	
	public void setTemplate(Template tpl){
		addOption("tpl",tpl);
	}
	
	public Template getTemplate(){
		return (Template)getOption("tpl");
	}
	
	@Override
	public Tag getAutoCreate(){
		Tag ac = new Tag();
		ac.setTag("input");
		ac.setType("text");
		ac.setSize(24);
		ac.setAutocomplete("off");
		return getOption("autoCreate") == null ? ac : (Tag)getOption("autoCreate");
	}
	
	public void setDisplayField(String displayField){
		addOption("displayField", displayField);
	}
	
	public String getDisplayField(){
		return getString("displayField", "");
	}
	
	public void setForceSelection(Boolean forceSelection){
		addOption("forceSelection", forceSelection);
	}
	
	public Boolean isForceSelection(){
		return getBoolean("forceSelection", false);
	}
	
	public void setHandleHeight(Integer handleHeight){
		addOption("handleHeight", handleHeight);
	}
	
	public Integer getHandleHeight(){
		return getInteger("handleHeight", 8);
	}
	
	public void setHiddenId(String hiddenId){
		addOption("hiddenId", hiddenId);
	}

	public String getHiddenId(){
		return getString("hiddenId","");
	}
	
	public void setHiddenName(String hiddenName){
		addOption("hiddenName", hiddenName);
	}
	
	public String getHiddenName(){
		return getString("hiddenName", "");
	}
	
	public void setHiddenValue(Object hiddenValue){
		addOption("hiddenValue", hiddenValue);
	}
	
	public Object getHiddenValue(){
		return getString("hiddenValue","");
	}
	
	public void setItemSelector(String itemSelector){
		addOption("itemSelector", itemSelector);
	}
	
	public String getItemSelector(){
		return getString("itemSelector","");
	}
	
	public void setLazyInit(Boolean lazyInit){
		addOption("lazyInit", lazyInit);
	}
	
	public Boolean isLazyInit(){
		return getBoolean("lazyInit", true);
	}
	
	public void setLazyRender(Boolean lazyRender){
		addOption("lazyRender", lazyRender);
	}
	
	public Boolean isLazyRender(){
		return getBoolean("lazyRender", false);
	}
	
	public void setListAlign(String listAlign){
		addOption("listAlign", listAlign);
	}
	
	public String getListAlign(){
		return getString("listAlign","tl-bl?");
	}
	
	public void setListClass(String listClass){
		addOption("listClass", listClass);
	}
	
	public String getListClass(){
		return getString("listClass", "");
	}
	
	public void setListEmptyText(String listEmptyText){
		addOption("listEmptyText", listEmptyText);
	}
	
	public String getListEmptyText(){
		return getString("listEmptyText", "");
	}
	
	public void setListWidth(Integer listWidth){
		addOption("listWidth", listWidth);
	}
	
	public Integer getListWidth(){
		return getInteger("listWidth",0);
	}
	
	public void setLoadingText(String loadingText){
		addOption("loadingText", loadingText);
	}
	
	public String getLoadingText(){
		return getString("loadingText", "Loading...");
	}
	
	public void setMaxHeight(Number maxHeight){
		addOption("maxHeight", maxHeight);
	}
	
	public Integer getMaxHeight(){
		return getInteger("maxHeight", 300);
	}
	
	public void setMinChars(Integer minChars){
		addOption("minChars", minChars);
	}
	
	public Integer getMinChars(){
		return getInteger("minChars", 24);
	}
	
	public void setMinHeight(Integer minHeight){
		addOption("minHeight", minHeight);
	}
	
	public Integer getMinHeight(){
		return getInteger("minHeight", 90);
	}
	
	public void setMinListWidth(Integer minListWidth){
		addOption("minListWidth", minListWidth);
	}
	
	public Integer getMinListWidth(){
		return getInteger("minListWidth", 70);
	}
	
	public void setMode(String mode){
		addOption("mode",mode);
	}
	
	public String getMode(){
		return getString("mode",MODE_REMOTE);
	}
	
	
	public void setPageSize(Integer pageSize){
		addOption("pageSize", pageSize);
	}
	
	public Integer getPageSize(){
		return getInteger("pageSize", 0);
	}
	
	public void setQueryDelay(Integer queryDelay){
		addOption("queryDelay", queryDelay);
	}
	
	public Integer getQueryDelay(){
		return getInteger("queryDelay", 0);
	}
	
	public void setQueryParam(String queryParam){
		addOption("queryParam", queryParam);
	}
	
	public String getQueryParam(){
		return getString("queryParam", "query");
	}
	
	public void setResizable(Boolean resizable){
		addOption("resizable", resizable);
	}
	
	public Boolean isResizable(){
		return getBoolean("resizable",false);
	}
	
	public void setSelectedClass(String selectedClass){
		addOption("selectedClass", selectedClass);
	}
	
	public String getSelectedClass(){
		return getString("selectedClass", "x-combo-selected");
	}
	
	public void setShadow(Object shadow){
		addOption("shadow", shadow);
	}
	
	public Object getShadow(){
		return getOption("shadow") == null ? "sides" : getOption("shadow");
	}
	
	
	public void setStore(Store store){
		addOption("store",store);
	}
	
	public Store getStore(){
		return (Store)getOption("store");
	}
	
	public String getTriggerClass(String triggerClass){
		return getString("triggerClass", "x-form-arrow-trigger");
	}
	
	public void setTypeAhead(Boolean typeAhead){
		addOption("typeAhead", typeAhead);
	}	
	
	public Boolean isTypeAhead(){
		return getBoolean("typeAhead", false);
	}
	
	public void setTypeAheadDelay(Integer typeAheadDelay){
		addOption("typeAheadDelay",typeAheadDelay);
	}

	public Integer getTypeAheadDelay(){
		return getInteger("typeAheadDelay",250);
	}
	
	public void setValueField(String valueField){
		addOption("valueField", valueField);
	}
	
	public String getValueField(){
		return getString("valueField", "");
	}
	
	public void setValueNotFoundText(String valueNotFoundText){
		addOption("valueNotFoundText", valueNotFoundText);
	}
	
	public String getValueNotFoundText(){
		return getString("valueNotFoundText", "");
	}
	
	public void setTriggerAction(String triggerAction){
		addOption("triggerAction", triggerAction);
	}
	
	public String getTriggerAction(){
		return getString("triggerAction",TRIGGER_ACTION_QUERY);
	}
	
	@Override
	public String getScript() {
		script = new StringBuffer("");
		createItems();
		script.append("new Ext.form.ComboBox(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}
	
}
